package com.planet.events;

import java.util.ArrayList;
import com.planet.classes.Event;
import com.planet.events.ImageManager;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class EventAdapter extends ArrayAdapter<Event> {
	private LayoutInflater _mInflater;
	private Activity _activity;
	private ArrayList<Event> _items;
	public ImageManager _iManager;

	public EventAdapter(Activity a, ArrayList<Event> items) {
		super(a, R.layout.eventlistitem, items);
		_activity = a;
		_mInflater = LayoutInflater.from(_activity);		
		_items = items;
		_iManager = new ImageManager(_activity.getApplicationContext());
	}

	@Override
	public int getCount() {
		return _items.size();
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			convertView = _mInflater.inflate(R.layout.eventlistitem, parent,false);
			holder = new ViewHolder();
			holder.image = (ImageView) convertView.findViewById(R.id.image);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.detail = (TextView) convertView.findViewById(R.id.detail);
			holder.progress = (ProgressBar) convertView.findViewById(R.id.progress_bar);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Event e = _items.get(pos);

		if (e.get_title().length() > 30)
			holder.title.setText(e.get_title().substring(0, 28) + "...");
		else
			holder.title.setText(e.get_title());

		holder.detail.setText(e.get_date());		

		String new_url = e.get_image().replaceAll(" ", "%20");

		holder.image.setTag(new_url);
		_iManager.displayImage(new_url, _activity, holder.image, holder.progress);
		return convertView;
	}

	static class ViewHolder {
		ImageView image;
		TextView title;
		TextView detail;
		ProgressBar progress; //ADDED
	}
}